package org.fuxian.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.fuxian.dao.ViolationMapper;
import org.fuxian.model.Violation;
import org.fuxian.model.vo.ViolationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/violation")
public class ViolationController {

    @Autowired
    ViolationMapper violationMapper;

    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(@RequestParam(value = "page", defaultValue = "1") Integer page) {
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("code", "200");

        PageHelper.startPage(page, 5);
        List<ViolationVo> violationList = violationMapper.selectAllVo();
        PageInfo<ViolationVo> pageInfo = new PageInfo<>(violationList, 5);
        returnMap.put("pageInfo", pageInfo);
        return returnMap;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String, Object> update(@RequestBody(required = true) Violation violation) {
        Map<String, Object> map = new HashMap<>();
        int count = violationMapper.updateByPrimaryKey(violation);
        if (count > 0) {
            map.put("code", "200");
        } else {
            map.put("code", "500");
        }
        return map;
    }

    @RequestMapping("/ts")
    @ResponseBody
    public Map<String, Object> ts(@RequestParam(value = "id") Integer id, @RequestParam(value = "flag") Integer flag) {
        Map<String, Object> map = new HashMap<>();
        Violation violation = violationMapper.selectByPrimaryKey(id);
        violation.setFlag(flag);
        int count = violationMapper.updateByPrimaryKey(violation);
        if (count > 0) {
            map.put("code", "200");
        } else {
            map.put("code", "500");
        }
        return map;
    }



    @RequestMapping("/del")
    @ResponseBody
    public Map<String, Object> delete(@RequestParam(value = "id") Integer id) {
        Map<String, Object> map = new HashMap<>();
        int count = violationMapper.deleteByPrimaryKey(id);
        if (count > 0) {
            map.put("code", "200");
        } else {
            map.put("code", "500");
        }
        return map;
    }

}
